package com.erp.servlet;

import com.erp.bean.Commodity;
import com.erp.bean.Emp;
import com.erp.bean.Order;
import com.erp.bean.OrderState;
import com.erp.dao.*;
import com.erp.service.CommodityService;
import com.erp.service.EmpService;
import com.erp.service.OrderService;
import com.erp.service.OrderStateService;
import com.erp.util.factroy.ObjectFactory;
import com.erp.util.spilt.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lpy
 * 订单列表显示控制器
 */
@WebServlet("/orderlist.action")
public class OrderListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = ObjectFactory.getServiceInstance("com.erp.service.impl.OrderServiceImpl", OrderService.class);
        EmpService empService = ObjectFactory.getServiceInstance("com.erp.service.impl.EmpServiceImpl",EmpService.class);
        CommodityService commodityService = ObjectFactory.getServiceInstance("com.erp.service.impl.CommodityServiceImpl", CommodityService.class);
        OrderStateService stateService = ObjectFactory.getServiceInstance("com.erp.service.impl.OrderStateServiceImpl", OrderStateService.class);

        List<Emp>  emps = empService.finAll();
        List<Commodity>  commodities = commodityService.findAll();
        List<OrderState>  states = stateService.findAll();

        //获取表单参数
        String colum = req.getParameter("search_field")==null?"gname":req.getParameter("search_field");
        String key = req.getParameter("keyword")==null?"":req.getParameter("keyword");
        System.out.println(colum);
        System.out.println(key);
        //查询总记录数
        long count = orderService.getAllCount(colum,key);
        //获取分页参数
        PageUtil pageUtil = new PageUtil();
        //设置最大记录数
        pageUtil.setTotalCount(count);
        //获取当前页
        String cp = req.getParameter("cp");
        //默认当前是第一页
        int currentPage = 1;
        if (cp!=null&&!cp.equals("")){
            currentPage = Integer.valueOf(cp);
            //当前页小于1
            if (currentPage<1){
                currentPage = 1;
            }
            //当前页大于最大页数
            if (currentPage>pageUtil.getTotalPage()){
                currentPage = (int)pageUtil.getTotalPage();
            }
            pageUtil.setCurrentPage(currentPage);
        }

        List<Order>  orders = orderService.findAllSpilt(pageUtil.getCurrentPage(),pageUtil.getPageCount(),colum,key);
        //将订单列表保存到request
        req.setAttribute("orders",orders);
        req.setAttribute("emps",emps);
        req.setAttribute("commodities",commodities);
        req.setAttribute("states",states);
        req.setAttribute("pageUtil",pageUtil);
        req.setAttribute("search_field",colum);
        req.setAttribute("content",key);
        //转发到列表页面显示
        req.getRequestDispatcher("orderslist.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
